// pages/StudiesTravel/StudiesTravel.js
var app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: [], //关键字
    tabList: [],
    tabItem: {},
    currentTab: 1,
    show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: "", //下拉列表的数据
    index: 0, //选择的下拉列表下标,
    cityId: 0
  },
  selectTap() {
    this.setData({
      show: !this.data.show
    });
  },
  /**
   * 获取课程类型
   */
  getclass:function(){
    let that = this;
    wx.request({
      url: app.httpUrl + '/home/queryCourseType',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: res => {
        console.log(res.data.data)
        that.setData({
          navbar:res.data.data
        })
      }
    })
  },
  // 点击下拉列表
  optionTap(e) {
    let index = e.currentTarget.dataset.index;
    let isLoad = false;
    if (this.data.cityId != index) {
      isLoad = true;
    }
    this.setData({
      cityId: index,
      show: !this.data.show
    });
    if (isLoad) {
      this.setData({
        tabList: [],
        tabItem: {}
      })
      this.queryCourseList();
    }
  },
  /**跳转 */
  jump: function (e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/programme/programme?productid=' + id,
    })
  },
  navbarTap: function (e) {
    wx.showToast({
      title: "加载中...",
      icon: "loading",
      mask: true,
      duration: 1500,
    })
    var key = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: key
    })
    let tabItem = this.data.tabList[key]
    if (!tabItem) {
      this.queryCourseList();
    } else {
      this.setData({
        tabItem: tabItem
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getclass();
    wx.showToast({
      title: "加载中...",
      icon: "loading",
      mask: true,
      duration: 1500,
    })
    if (options.courseType != undefined) {
      this.setData({
        currentTab: options.courseType
      })
    }
    this.queryCompanyCity();
    this.queryCourseList();
  },
  loadMore: function () {
    wx.showToast({
      title: "加载中...",
      icon: "loading",
      mask: true,
      duration: 1500,
    })
    this.queryCourseList();
  },
  queryCompanyCity: function () {
    let that = this;
    // 首页头部地区接口
    wx.request({
      url: app.httpUrl + '/home/queryCompanyCity',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: res => {
        var list = res.data.data;
        list.unshift({
          "dictDataKey": "1",
          "dictDataValue": "城市"
        });
        that.setData({
          selectData: list
        })
      }
    })
  },
  queryCourseList: function () {
    var that = this;
    var tabItem = that.data.tabList[that.data.currentTab];
    if (!tabItem) {
      tabItem = {
        page: 1,
        rows: 10,
        itemList: [],
        isOver: false
      };
    }
    wx.request({
      url: app.httpUrl + '/course/queryCourseList',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        courseType: that.data.currentTab,
        cityId: that.data.cityId > 0 ? that.data.cityId : null,
        page: tabItem.page,
        rows: tabItem.rows
      },
      success: res => {
        console.log(res.data.data)
        if (res.data.code == '000') {
          if (tabItem.page >= res.data.data.totalPage) {
            tabItem.isOver = true;
          }
          tabItem.page += 1;
          var list = res.data.data.results;
          for (var i in list) {
            list[i].startD = util.formatDate(list[i].startDate, 'M月D日');
            list[i].endD = util.formatDate(list[i].endDate, 'M月D日');
            tabItem.itemList.push(list[i]);
            var keyword = list[i].courseKeyword ? list[i].courseKeyword.split("、") : "";
            var arr = []//新关键字数组
            list[i].keyword = keyword;
            for (let a = 0; a < list[i].keyword.length; a++) {
              if (a == 3) {
                break
              }
              arr.push(list[i].keyword[a])
            }
            list[i].keyword = arr;
          }
          that.data.tabList[that.data.currentTab] = tabItem;
          that.setData({
            tabList: that.data.tabList,
            tabItem: tabItem
          })
        }
        wx.hideToast()

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
})